﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NUnit.Framework;
using Farigola.Engine.SystemData.Entity.inst;
using Farigola.Engine.SystemData.Entity;
using Farigola.Engine.SystemData.Entity.Type;

namespace Farigola.Engine.TestUnit
{
    [TestFixture]
    public class ProjectTestUnit
	{
        //---------------------------------------------------------------------
        /// <summary>
        /// Create a project with id and name ok.
        /// </summary>
        [Test]
	    public void testCreateProjectIdNameOk() 
        {
		    Engine Engine= EngineCommonTestUnit.CreateInitEngine();
		
		    Workspace wksp= Engine.GetWorkspace();
		
		    // create an empty project
		    Project prj= wksp.CreateActiveProject("prj");
		    Assert.IsNotNull(prj, "The project should exists.");
	    }

        //---------------------------------------------------------------------
        /// <summary>
        /// Create a project with id and name ok.
        /// </summary>
        [Test]
	    public void testCreateProjectIdNameWrong() 
        {
            Engine Engine = EngineCommonTestUnit.CreateInitEngine();
		
		    Workspace wksp= Engine.GetWorkspace();
		
		    // create an empty project
		    Project prj;
		    prj= wksp.CreateActiveProject(null);
		    Assert.IsNull(prj, "The name is wrong (null), the project should be not created.");
		
		    prj= wksp.CreateActiveProject("  ");
		    Assert.IsNull(prj, "The name is wrong (blank), the project should be not created.");
	    }

        //---------------------------------------------------------------------
        /// <summary>
        /// Create a project, check the internal structure.
        /// </summary>
        [Test]
	    public void testCreateProjectCheckInternal() 
        {
            Engine Engine = EngineCommonTestUnit.CreateInitEngine();
		
		    Workspace wksp= Engine.GetWorkspace();
		
		    // create an empty project
		    Project prj= wksp.CreateActiveProject("prj");
            string errListCode;
            bool res = prj.CheckInternal(out errListCode);
		    Assert.IsNotNull(res,"The checkInternal of the project should be ok.");
            Assert.AreEqual("", errListCode, "The checkInternal shoufl set no error code.");
		
		    res= prj.GetProjectStruct().CheckInternalAllRootFolder();
		    Assert.IsNotNull(res,"The checkInternalAllRoot of the project should be ok.");
	    }

        //---------------------------------------------------------------------
        /// <summary>
        /// Create a project, check the internal structure.
        /// </summary>
        [Test]
	    public void testCreateProjectRootFolder() 
        {
            Engine Engine = EngineCommonTestUnit.CreateInitEngine();
		
		    Workspace wksp= Engine.GetWorkspace();
		
		    // create the active project
		    Project prj= wksp.CreateActiveProject("prj");

		    EntInstFolder rootFolder= prj.GetRootFolder();
		    Assert.IsNotNull(rootFolder, "The root folder of the project should exists.");

		    //--check the rootFolder
		    // check the typeOrUser, and the name 
            Assert.AreEqual(EntityDef.TypeSystOrUser.System, rootFolder.GetTypeSystOrUser(), "The TypeOrUser should be System for the rootFolder.");
            Assert.AreEqual(EntityDef.prjRootFolderName, rootFolder.GetName(), "The name should be the defined name for the rootFolder.");
            Assert.IsNotNull(rootFolder.GetId(), "The id should be not null for the rootFolder.");		
		    // the rootFolder has two 2 childs
            Assert.AreEqual(2, rootFolder.ChildCount(), "The rootFolder should have 2 childs.");
		
		    // get the root folder user, contains all entities adds by the user
		    EntInstFolder rootUserFolder= prj.GetRootUserFolder();
            Assert.IsNotNull(rootUserFolder, "The root folder user of project should exists.");
            Assert.AreEqual(rootFolder, rootUserFolder.GetParentFolder(), "The parent of the rootFolderUser should be the root folder.");
		
		    //--check the rootFolderSystem
		    EntInstFolder rootSystemFolder= prj.GetRootSystemFolder();
            Assert.IsNotNull(rootSystemFolder, "The root folder system of project should exists.");
            Assert.AreEqual(rootFolder, rootSystemFolder.GetParentFolder(), "The parent of the rootFolderSystem should be the root folder.");
		
		    //--other check, get childs of the root folder
		    List<EntityBase> listEnt=rootFolder.GetListChild();
		    rootSystemFolder= (EntInstFolder) rootFolder.FindChildByName(EntityDef.prjRootSystemFolderName); 
		    rootUserFolder= (EntInstFolder) rootFolder.FindChildByName(EntityDef.prjRootUserFolderName); 

		    // check detail of root folder user
		    Assert.AreEqual(EntityDef.TypeSystOrUser.System, rootUserFolder.GetTypeSystOrUser(),"The TypeOrUser should be System for the rootFolderUser.");
            Assert.AreEqual(EntityDef.prjRootUserFolderName, rootUserFolder.GetName(), "The name should be the defined name for the rootFolderUser.");
            Assert.IsNotNull(rootUserFolder.GetId(), "The id should be not null for the rootFolderUser.");
		    // has no child
            Assert.AreEqual(0, rootUserFolder.ChildCount(), "The rootFolderUser shouldn't have child.");
		
		    // check detail of root folder system
            Assert.AreEqual(EntityDef.TypeSystOrUser.System, rootSystemFolder.GetTypeSystOrUser(), "The TypeOrUser should be System for the rootFolderSystem.");
            Assert.AreEqual(EntityDef.prjRootSystemFolderName, rootSystemFolder.GetName(), "The name should be the defined name for the rootFolderSystem.");
            Assert.IsNotNull(rootSystemFolder.GetId(), "The id should be not null for the rootFolderSystem.");
		    // has no child
            Assert.AreEqual(0, rootSystemFolder.ChildCount(), "The rootFolderUser shouldn't have child.");
	    }

        //---------------------------------------------------------------------
        /// <summary>
        /// Create an empty project, used for load.
        /// </summary>
        //[Test]
	    public void testCreateProjectEmpty() 
        {
            Engine Engine = EngineCommonTestUnit.CreateInitEngine();
		
		    Workspace wksp= Engine.GetWorkspace();
		
		    // create an empty project
		    //Project prj= wksp.createEmptyProject(name); 
				
		    // set an id and a name
		    // todo:
					
		    // then, becomes the active project
		    // todo:
		
		    //fail("todo:");
	    }

            //---------------------------------------------------------------------
            // create an empty project with wrong name and wrong id
	    // todo:


	}
}
